home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 April / macformat-049.iso / mac / Shareware Plus / Developers / dropg++ / usr / src / loader / standalone.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-15  |  5.9 KB  |  258 lines  |  [TEXT/KAHL]

  1. #include <Files.h>
  2. #include <AppleEvents.h>
  3. #include <Menus.h>
  4. #include <Windows.h>
  5. #include <ToolUtils.h>
  6. #include <Desk.h>
  7. #include <SegLoad.h>
  8. #include <OSUtils.h>
  9. #include <Memory.h>
  10. #include <Fonts.h>
  11. #include <Events.h>
  12. #include <OSEvents.h>
  13. #include <TextEdit.h>
  14. #include <Dialogs.h>
  15. #include <Aliases.h>
  16. #include <Folders.h>
  17. #include <Errors.h>
  18. #include <GestaltEqu.h>
  19. #include <StandardFile.h>
  20.  
  21. #include <fcntl.h>
  22. #include <a.out.h>
  23. #include <sys/exec.h>
  24.  
  25. #define TrapMask 0x0800
  26.  
  27. static short NumToolboxTraps( void )
  28. {
  29.     if (GetToolTrapAddress(0xA86E) ==
  30.             GetToolTrapAddress(0xAA6E))
  31.         return(0x0200);
  32.     else
  33.         return(0x0400);
  34. }
  35.  
  36. static TrapType GetTrapType(short theTrap)
  37. {
  38.  
  39.     if ((theTrap & TrapMask) > 0)
  40.         return(ToolTrap);
  41.     else
  42.         return(OSTrap);
  43.  
  44. }
  45.  
  46. static Boolean TrapAvailable(short theTrap)
  47. {
  48.  
  49.     TrapType    tType;
  50.  
  51.     tType = GetTrapType(theTrap);
  52.     if (tType == ToolTrap)
  53.         theTrap = theTrap & 0x07FF;
  54.     if (theTrap >= NumToolboxTraps())
  55.         theTrap = 0xA89F;
  56.  
  57.     return ((tType == ToolTrap?GetToolTrapAddress(theTrap):GetOSTrapAddress(theTrap))!=
  58.             GetToolTrapAddress(0xA89F));
  59. }
  60.  
  61.  
  62. #define _CacheFlushTrap    0xA0BD
  63.  
  64. void FlushCache(void)
  65. {
  66.     if (TrapAvailable( _CacheFlushTrap))
  67.         asm
  68.         {
  69.             dc.w _CacheFlushTrap
  70.         }
  71. }
  72.  
  73. void fatal(char *str)
  74.     {
  75.     Str255    errmsg;
  76.     static struct  {
  77.         char privates[76];
  78.         long randSeed;
  79.         BitMap screenBits;
  80.         Cursor arrow;
  81.         Pattern dkGray;
  82.         Pattern ltGray;
  83.         Pattern gray;
  84.         Pattern black;
  85.         Pattern white;
  86.         GrafPtr thePort;
  87.     }myqd;    
  88.     InitGraf(&myqd.thePort);
  89.     InitFonts();
  90.     FlushEvents(everyEvent, 0);
  91.     InitWindows();
  92.     InitMenus();
  93.     TEInit();
  94.     InitDialogs(0L);
  95.     InitCursor();
  96.     *errmsg = strlen(str);
  97.     BlockMove(str, 1+errmsg, *errmsg);
  98.     ParamText("\pfatal error ", errmsg, 0,0);
  99.     Alert(129, 0);
  100.     SysBeep(1);
  101.     ExitToShell();
  102.     }
  103.  
  104. void *emptyblock(size_t sz)
  105.     {
  106.     void *ptr = NewPtrClear(sz);
  107.     if (!ptr) fatal("Out of memory");
  108.     return ptr;
  109.     }
  110.  
  111. int read(int fd, void *buf, unsigned size)
  112.         {
  113.         int i;
  114.         OSErr err;
  115.         IOParam pb;
  116.         pb.ioCompletion = 0;
  117.         pb.ioRefNum = fd;
  118.         pb.ioBuffer = buf;
  119.         pb.ioReqCount = size;
  120.         pb.ioPosMode = fsAtMark;
  121.         PBReadSync((ParmBlkPtr)&pb);
  122.         err = (pb.ioResult);
  123.         return pb.ioActCount;
  124.         }
  125.  
  126. int newproc(FSSpec name1, int resource_size)
  127.         {
  128.         void (*entry)();
  129.         struct exec header;
  130.         OSErr err;
  131.         int fd,refnum;
  132.         HParamBlockRec pb;
  133.         if (!*name1.name) return -1;
  134.         pb.ioParam.ioNamePtr = name1.name;
  135.         pb.ioParam.ioVRefNum = name1.vRefNum;
  136.         pb.fileParam.ioDirID = name1.parID;
  137.         pb.ioParam.ioPermssn = fsRdPerm;
  138.         err = (PBHOpenSync(&pb));
  139.         if ( err ) return -1;
  140.         refnum = pb.ioParam.ioRefNum;
  141.         if (refnum < 0) fatal("Could not open exec file");
  142.         read(refnum, (void *)&(header), sizeof(header));
  143.         if (N_BADMAG(header)) fatal("Bad magic number");
  144.         if (header.a_trsize || header.a_drsize)
  145.             {
  146.             void *storage;
  147.             long stack_headroom = 1<<16;
  148.             resource_size = loader_main(refnum, &(header),resource_size);
  149.             if (resource_size == -1) fatal("Could not load program");
  150.             }
  151.         else fatal("Program not relocatable");
  152.             {
  153.             FCBPBRec pb;
  154.             pb.ioRefNum = refnum;
  155.             pb.ioCompletion = 0;
  156.             pb.ioVRefNum = name1.vRefNum;
  157.             PBCloseSync((ParmBlkPtr)&pb);
  158.             }
  159.         entry = (void (*)())(header.a_entry);
  160.         FlushCache();
  161.         entry();
  162.         ExitToShell();
  163.         }
  164.  
  165. OSErr   CurrentProcessLocation(FSSpec *applicationSpec)
  166. {
  167.     ProcessSerialNumber currentPSN;
  168.     ProcessInfoRec info;
  169.     
  170.     currentPSN.highLongOfPSN = 0;
  171.     currentPSN.lowLongOfPSN = kCurrentProcess;
  172.     info.processInfoLength = sizeof(ProcessInfoRec);
  173.     info.processName = NULL;
  174.     info.processAppSpec = applicationSpec;
  175.     return ( GetProcessInformation(¤tPSN, &info) );
  176. }
  177.  
  178. void main(void)
  179.         {
  180.         FSSpec applicationSpec;
  181.         OSErr err = CurrentProcessLocation(&applicationSpec);
  182.         CInfoPBRec  cPB;
  183.         cPB.hFileInfo.ioNamePtr = applicationSpec.name;
  184.         cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
  185.         cPB.hFileInfo.ioDirID = applicationSpec.parID;
  186.         cPB.hFileInfo.ioFDirIndex = 0;
  187.  
  188.         if (!PBGetCatInfoSync(&cPB))
  189.             {
  190.             long size = cPB.hFileInfo.ioFlLgLen;
  191.             if (size) newproc(applicationSpec, size);
  192.             else
  193.                 {
  194.                 static struct  {
  195.                     char privates[76];
  196.                     long randSeed;
  197.                     BitMap screenBits;
  198.                     Cursor arrow;
  199.                     Pattern dkGray;
  200.                     Pattern ltGray;
  201.                     Pattern gray;
  202.                     Pattern black;
  203.                     Pattern white;
  204.                     GrafPtr thePort;
  205.                 }myqd;
  206.                 long        aProcID;
  207.                   SFReply macSFReply;
  208.                 Point    where;
  209.                 SFTypeList    typeList;
  210.                 
  211.                 InitGraf(&myqd.thePort);
  212.                 InitFonts();
  213.                 FlushEvents(everyEvent, 0);
  214.                 InitWindows();
  215.                 InitMenus();
  216.                 TEInit();
  217.                 InitDialogs(0L);
  218.                 InitCursor();
  219.                         
  220.                 typeList[0]='TEXT';    /* file type to search for */
  221.                 typeList[1]='ŎŎ';    /* file type to search for */
  222.                 where.h=20;  where.v=90;    /* SF dialog window position */
  223.             
  224.                 SFGetFile(where,        /* put topLeft corner of dialog box here */
  225.                             (ConstStr255Param)0,            /* always 0 on SFGetFile() */
  226.                             0,            /* address of custom filter */
  227.                             -1,            /* looking for all file types */
  228.                             typeList,    /* start of list of file types */
  229.                             0,     /* use standard dialog handler */
  230.                             &macSFReply);    /* put results here */
  231.  
  232.                 if ( macSFReply.good ) 
  233.                     { 
  234.                     WDPBRec    wdpb;
  235.                     wdpb.ioNamePtr = 0;
  236.                     wdpb.ioVRefNum = macSFReply.vRefNum;
  237.                     wdpb.ioWDIndex =0;
  238.                     wdpb.ioWDProcID =0;
  239.                     wdpb.ioWDVRefNum =0;
  240.                     PBGetWDInfoSync( &wdpb );
  241.                     applicationSpec.parID = wdpb.ioWDDirID;
  242.                     applicationSpec.vRefNum = wdpb.ioWDVRefNum;
  243.                       BlockMove(macSFReply.fName, applicationSpec.name, macSFReply.fName[0]+1);
  244.                     cPB.hFileInfo.ioNamePtr = applicationSpec.name;
  245.                     cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
  246.                     cPB.hFileInfo.ioDirID = applicationSpec.parID;
  247.                     cPB.hFileInfo.ioFDirIndex = 0;
  248.             
  249.                     if (!PBGetCatInfoSync(&cPB))
  250.                         {
  251.                         long size = cPB.hFileInfo.ioFlLgLen;
  252.                           newproc(applicationSpec, size);
  253.                           }
  254.                     }
  255.                 }
  256.             }
  257.         }
  258.